package com.jenvin.activemq.topic;

import java.util.Arrays;

import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * Use in conjunction with TopicListener to test the performance of ActiveMQ Topics.
 */
public class TopicPublisher {

    private String url       = "tcp://192.168.1.62:61616";
    private String topicName = "topic.test.ask2";
    private String message   = "";

    public void run() throws JMSException {
        TopicConnectionFactory connFactory = new ActiveMQConnectionFactory(url);
        TopicConnection connection = connFactory.createTopicConnection();
        TopicSession session = connection.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
        Topic topic = session.createTopic(topicName);
        MessageProducer publisher = session.createProducer(topic);
        connection.start();
        String text = "TextMessage" + message;
        TextMessage msg = session.createTextMessage(text);
        publisher.send(msg);
        System.out.println("publisher message:" + text);
        connection.stop();
        connection.close();
    }

    public static void main(String[] argv) throws Exception {
        TopicPublisher l = new TopicPublisher();
        String[] unknown = CommandLineSupport.setOptions(l, argv);
        if (unknown.length > 0) {
            System.out.println("Unknown options: " + Arrays.toString(unknown));
            System.exit(-1);
        }
        l.run();
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTopicName() {
        return topicName;
    }

    public void setTopicName(String topicName) {
        this.topicName = topicName;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
